package com.zhaowb.springcloud.admin.rest;

import com.github.pagehelper.PageHelper;
import com.zhaowb.springcloud.admin.biz.UserBiz;
import com.zhaowb.springcloud.admin.entity.User;
import com.zhaowb.springcloud.admin.mapper.UserMapper;
import com.zhaowb.springcloud.common.msg.TableResultResponse;
import com.zhaowb.springcloud.common.rest.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created with IDEA
 *
 * @author zhaowb
 * @date 2018/11/1 21:45
 */
@Controller
@RequestMapping("user")
public class UserController extends BaseController<UserBiz, User> {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/page")
    @ResponseBody
    public TableResultResponse<User> page(@RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1") int offset, String name) {

        Example example = new Example(User.class);
        if (StringUtils.isNoneBlank(name)) {
            example.createCriteria().andLike("name", "%" + name + "%");
            example.createCriteria().andLike("username", "%" + name + "%");
        }
        int count = baseBiz.selectCountByExample(example);
        PageHelper.startPage(offset, limit);

        return new TableResultResponse<>(count, baseBiz.selectByExample(example));
    }

    @RequestMapping(value = "/getListUser")
    @ResponseBody
    public List<User> getListUser() {
        return baseBiz.selectListAll();
    }

    @RequestMapping(value = "/getUserByUsername")
    @ResponseBody
    public User getUserByUsername(String username) {
        return baseBiz.getUserByUsername(username);
    }

    @ResponseBody
    @RequestMapping(value = "/getUsers", method = RequestMethod.GET)
    public List<User> getUsers(int groupId) {
        return userMapper.selectMemberByGroupId(groupId);
    }
}
